﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BattleShipPro
{
    /// <summary>
    /// Interaction logic for OpponentStatusControl.xaml
    /// </summary>
    public partial class OpponentStatusControl : UserControl
    {
        public event EventHandler<LocationEventArgs> Attacked;
        public OpponentStatusControl()
        {
            InitializeComponent();
            for (int i = 0; i < Constants.BoardSize; i++)
            {
                mainGrid.RowDefinitions.Add(new RowDefinition());
                mainGrid.ColumnDefinitions.Add(new ColumnDefinition());
            }
        }

        void BlockMouseDown(object sender, MouseButtonEventArgs e)
        {
            var block = sender as Border;
            var x = Grid.GetColumn(block);
            var y = Grid.GetRow(block);
            
            var fire = new Fire();
            Grid.SetColumn(fire, x);
            Grid.SetRow(fire, y);
            mainGrid.Children.Add(fire);

            if (Attacked != null)
                Attacked(this, new LocationEventArgs() { X = x, Y = y });
        }

        public void Clear()
        {
            mainGrid.Children.Clear();
        }

        public void LoadGame(Session computerSession)
        {
            Clear();

            for (int i = 0; i < Constants.BoardSize; i++)
            {
                for (int j = 0; j < Constants.BoardSize; j++)
                {
                    var block = new Border();
                    block.Background = Brushes.Transparent;
                    block.HorizontalAlignment = HorizontalAlignment.Stretch;
                    block.VerticalAlignment = VerticalAlignment.Stretch;
                    Grid.SetColumn(block, i);
                    Grid.SetRow(block, j);
                    mainGrid.Children.Add(block);
                    block.MouseDown += BlockMouseDown;
                }
            }
        }
    }
}
